package com.liujit.standard.basis.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liujit.standard.basis.admin.model.SysLog;
import com.liujit.standard.basis.admin.service.SysLogService;
import com.liujit.standard.basis.core.util.CoreUtil;
import com.liujit.standard.basis.admin.config.annotation.Auth;
import com.liujit.standard.basis.admin.config.annotation.Log;
import com.liujit.standard.basis.admin.domain.query.SysLogQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 日志管理
 * @Author LiuJun
 * @Date 2021-03-26 09:19:24
 */
@Api(value = "日志管理接口", tags = "日志管理接口")
@EnableTransactionManagement
@RestController
@RequestMapping("/admin/sys/log")
public class SysLogController {

    @Resource
    private SysLogService sysLogService;

    @Auth("sysLog-sel")
    @ApiOperation(value = "查询列表", notes = "查询列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysLogQuery")})
    @GetMapping("/getList")
    public List<SysLog> getList(SysLogQuery query) {
        CoreUtil.Paging(query, "create_time desc");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type", query.getType());
        if (StrUtil.isNotEmpty(query.getUserName())) {
            wrapper.like("user_name", query.getUserName());
        }
        if (StrUtil.isNotEmpty(query.getActionUrl())) {
            wrapper.like("action_url", query.getActionUrl());
        }
        if (query.getResultCode() != null) {
            wrapper.eq("result_code", query.getResultCode());
        }
        return sysLogService.list(wrapper);
    }

    @Log
    @Auth("sysLog-add")
    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysLog")})
    @PostMapping("/add")
    public Boolean add(@RequestBody SysLog param) {
        return sysLogService.save(param);
    }

    @Log
    @Auth("sysLog-upd")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @PostMapping("/upd")
    public Boolean upd(@RequestBody SysLog param) {
        return sysLogService.updateById(param);
    }

    @Auth("sysLog-sel")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @GetMapping("/get")
    public SysLog get(Long id) {
        return sysLogService.getById(id);
    }

    @Log
    @Auth("sysLog-del")
    @ApiOperation(value = "删除/批量删除", notes = "删除/批量删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = ",分割的id")})
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(@RequestBody JSONObject param) {
        String id = param.getString("id");
        List<String> ids = Arrays.asList(id.split(","));
        return sysLogService.removeByIds(ids);
    }
}
